package com.ruoyi.web.controller.user;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysRight;
import com.ruoyi.system.service.ISysRightService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysGroup;
import com.ruoyi.system.service.ISysGroupService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-10-21
 */
@Controller
@RequestMapping("/system/group")
public class SysGroupController extends BaseController
{
    private String prefix = "system/group";

    @Autowired
    private ISysGroupService sysGroupService;
    @Autowired
    private ISysRightService sysRightService;

    @RequiresPermissions("system:group:view")
    @GetMapping()
    public String group()
    {
        return prefix + "/group";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:group:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysGroup sysGroup)
    {
        startPage();
        List<SysGroup> list = sysGroupService.selectSysGroupList(sysGroup);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:group:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysGroup sysGroup)
    {
        List<SysGroup> list = sysGroupService.selectSysGroupList(sysGroup);
        ExcelUtil<SysGroup> util = new ExcelUtil<SysGroup>(SysGroup.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:group:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysGroup sysGroup)
    {
        return toAjax(sysGroupService.insertSysGroup(sysGroup));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:group:edit")
    @GetMapping("/edit/{groupid}")
    public String edit(@PathVariable("groupid") Long groupid, ModelMap mmap)
    {
        SysGroup sysGroup = sysGroupService.selectSysGroupByGroupid(groupid);
        mmap.put("sysGroup", sysGroup);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:group:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysGroup sysGroup)
    {
        return toAjax(sysGroupService.updateSysGroup(sysGroup));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:group:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysGroupService.deleteSysGroupByGroupids(ids));
    }
    /**
     * 设置权限
     */
    @GetMapping("/privilege/{groupid}")
    public String privilege(ModelMap modelMap,@PathVariable("groupid") Long groupid)
    {
        List<SysRight> sysRightList = sysRightService.selectSysRightList(new SysRight());
        String rights = sysGroupService.selectSysGroupByGroupid(groupid).getRights();
        String stringarr[];
        int[] intArray = {0};
        if(!rights.isEmpty()) {
            stringarr = rights.split(",");
             intArray = new int[stringarr.length];
            for (int i = 0; i < stringarr.length; i++) {
                intArray[i] = Integer.parseInt(stringarr[i]);
            }
        }
        for(int i = 0;i<sysRightList.size();i++)
        {
            if(StringUtils.contains(intArray,sysRightList.get(i).getRightid().intValue()))
               sysRightList.get(i).setCheck(true);
            else
                sysRightList.get(i).setCheck(false);

        }
        modelMap.put("groupid",groupid);
        modelMap.put("sysright",sysRightList);
        return prefix + "/privilege";
    }
    @RequiresPermissions("system:group:privilege")
    @Log(title = "编辑权限", businessType = BusinessType.UPDATE)
    @PostMapping("/editprivilege")
    @ResponseBody
    public AjaxResult editprivilegeSave(SysGroup sysGroup)
    {
        return toAjax(sysGroupService.updateSysGroup(sysGroup));
    }
}
